package com.panda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panda.annotation.SystemLog;
import com.panda.entity.po.Comment;
import com.panda.entity.vo.CommentVo;
import com.panda.entity.vo.PageVo;
import com.panda.enums.AppHttpCodeEnum;
import com.panda.mapper.CommentMapper;
import com.panda.service.CommentService;
import com.panda.service.UserService;
import com.panda.utils.BeanCopyUtil;
import com.panda.utils.RespondResult;
import com.panda.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: panda
 * @Date: 2022-05-18 16:53
 * @Description: 评论service
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    /**
     * 获取文章评论
     * @param type
     * @param id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    @SystemLog(businessName = "文章评论")
    public RespondResult<PageVo<CommentVo>> getComment(String type, Long id, Integer pageNum, Integer pageSize) {

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //获取视频或文章评论id
        queryWrapper.eq(Comment::getOwner_id, id);
        //评论类型(视频或文章)
        queryWrapper.eq(Comment::getType, type);
        //查询根评论
        queryWrapper.eq(Comment::getRootId, -1);

        Page<Comment> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);

        //查询所有跟评论对应的子评论
        List<CommentVo> commentVoList = toCommentVoList(page.getRecords());
        //设置子评论
        for (CommentVo commentVo : commentVoList) {
            List<CommentVo> children = getChildren(commentVo.getId());
            commentVo.setChildren(children);
        }
        PageVo<CommentVo> commentVoPageVo = new PageVo<>(commentVoList, page.getTotal());
        return RespondResult.okResult(commentVoPageVo);
    }

    /**
     * 发表文章评论
     * @param type
     * @param id
     * @param commentVo
     * @return
     */
    @Override
    @SystemLog(businessName = "发表评论")
    public RespondResult<CommentVo> postComment(
            String type, Long id, CommentVo commentVo) {

        // 判断用户是否登陆
        Long userId = SecurityUtils.getUserId();
        if (userId != null) {
            //创建评论对象
            Comment comment = new Comment();
            comment.setOwner_id(id);
            comment.setType(type);
            comment.setRootId(commentVo.getRootId());
            comment.setToCommentId(commentVo.getToCommentId());
            comment.setToCommentUserId(commentVo.getToCommentUserId());
            comment.setContent(commentVo.getContent());
            //保存数据库
            boolean save = save(comment);
            if (save){
                return RespondResult.okResult(commentVo);
            }
        }
        return RespondResult.errorResult(AppHttpCodeEnum.POST_COMMENT_ERROR);
    }


    /**
     * 获取评论的子评论
     * @param id
     * @return
     */
    private List<CommentVo> getChildren(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId, id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> comments = list(queryWrapper);
        return toCommentVoList(comments);
    }

    /**
     * 批量添加评论用户名，评论对象用户名
     * @param list
     * @return
     */
    private List<CommentVo> toCommentVoList(List<Comment> list) {
        List<CommentVo> commentVos = BeanCopyUtil.copyBeans(list, CommentVo.class);
        for (CommentVo commentVo : commentVos) {
            String username = userService.getById(commentVo.getCreateBy()).getUsername();
            String avatar = userService.getById(commentVo.getCreateBy()).getAvatar();
            commentVo.setUsername(username);
            commentVo.setAvatar(avatar);
            if (commentVo.getToCommentUserId() != -1) {
                String toCommentUsername = userService.getById(commentVo.getToCommentUserId()).getUsername();
                commentVo.setToCommentUsername(toCommentUsername);
            }
        }
        return commentVos;
    }


}
